package cn.kk.utils;

import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.Getter;

import java.util.Random;

/**
 * 验证码工具
 * @author liujunkai
 * @date 2023/8/1 16:57
 */
public class CaptchaUtil {

    /**
     * 构建验证码对象
     * @param type 验证码类型
     * @return 验证码对象
     */
    public static Captcha buildCaptcha(CaptchaType type) {
        switch (type) {
            // 中文
            case CHINESE:
                return new ChineseCaptcha();
            // 中文gif
            case CHINESE_GIF:
                return new ChineseGifCaptcha();
            // 算数
            case ARITHMETIC:
                return new ArithmeticCaptcha();
            // 字符
            case CHARACTER:
                return new SpecCaptcha();
            // 动态字符
            default:
                return new GifCaptcha();
        }
    }

    /**
     * 构建验证码对象
     * @return 验证码对象
     */
    public static Captcha randomCaptcha() {
        CaptchaType[] enumItems = CaptchaType.class.getEnumConstants();
        int index = new Random().nextInt(enumItems.length);
        // 获取算数验证码
        return buildCaptcha(enumItems[index]);
    }

    @Getter
    public enum CaptchaType {
        CHINESE(1, "中文"),
        CHINESE_GIF(2, "中文gif"),
        ARITHMETIC(3, "算数"),
        CHARACTER(4, "字符"),
        CHARACTER_GIF(5, "动态字符");

        CaptchaType(int code, String description) {
            this.code = code;
            this.description = description;
        }

        private final int code;
        private final String description;
    }


}